package com.ecoolex.ying.space.api.graphql;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.alibaba.fastjson.JSONArray;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.ying.space.entity.OutsourcingGoodsWithBLOBs;
import com.ecoolex.ying.space.entity.OutsourcingPackaged;
import com.ecoolex.ying.space.entity.OutsourcingPackagedGoods;
import com.ecoolex.ying.space.entity.WholeHouseCase;
import com.ecoolex.ying.space.ws.OutsourcingGoodsService;
import com.ecoolex.ying.space.ws.WholeHouseCaseService;


@GraphQL2Module(type = Type.User, description = "外购商品套餐")
public class OutsourcingPackagedGraphql {

    @Autowired
    private OutsourcingGoodsService outsourcingGoodsService;

    @Autowired
    private StorageService storageService;

    @Autowired
    private WholeHouseCaseService wholeHouseCaseService;

    @TypeField(type = OutsourcingPackaged.class, field = "coverImage", description = "套餐列表图片")
    public StorageInfo coverImage(@Source OutsourcingPackaged source) {
        return storageService.get(source.getCoverImage());
    }

    @TypeField(type = OutsourcingPackaged.class, field = "images", description = "套餐图片")
    public List<StorageInfo> images(@Source OutsourcingPackaged source) {
        JSONArray array = JSONArray.parseArray(source.getImages());
        List<String> storageIdLsit = array.toJavaList(String.class);
        return storageIdLsit.stream().map(storageId -> storageService.get(storageId)).collect(Collectors.toList());
    }

    @TypeField(type = OutsourcingPackaged.class, field = "outsourcingGoodsList", description = "商品列表")
    public List<OutsourcingGoodsWithBLOBs> outsourcingGoods(@Source OutsourcingPackaged source) {
        List<OutsourcingPackagedGoods> list
            = outsourcingGoodsService.getOutsourcingPackagedGoodsByOutsourcingPackagedId(
                source.getOutsourcingPackagedId());
        return list.stream().map(outsourcingPackagedGoods -> outsourcingGoodsService.getOutsourcingGoodsById(
            outsourcingPackagedGoods.getOutsourcingGoodsId())).filter(Check::notNull).collect(Collectors.toList());
    }

    @TypeField(type = OutsourcingPackaged.class, field = "wholeHouseCase", description = "所属案例")
    public WholeHouseCase wholeHouseCase(@Source OutsourcingPackaged source) {
        return wholeHouseCaseService.getWholeHouseCaseByOutsourcingPackagedId(source.getOutsourcingPackagedId());
    }

    @QueryField(field = "outsourcingPackaged", description = "外购套餐详情")
    public OutsourcingPackaged get(@NonNull Integer outsourcingPackagedId) {
        return outsourcingGoodsService.getOutsourcingPackagedByIdForH5(outsourcingPackagedId);
    }

    @QueryField(field = "outsourcingPackagedList", description = "外购套餐列表")
    public Pagination<OutsourcingPackaged> query(@NonNull Sorting[] sorts, @NonNull Page page) {
        return outsourcingGoodsService.queryOutsourcingPackagedForH5(sorts, page);
    }
}
